#!/usr/bin/env python
import logging
from binance.um_futures import UMFutures
from binance.lib.utils import config_logging
from binance.error import ClientError

config_logging(logging, logging.DEBUG)

key = "z4U7noE4iEkLPVi3z02yq4iKvnUkeOslYXRzNKQhFFMni3dzsjmdjfQXZWvuS9Am"
secret = "mEEpNcWyeSEcHbvkpPTPGJRWgTX0iS7QjCA2aaMHUTgyOuCRPTm8Y7HByPnPaWQ5"

um_futures_client = UMFutures(key=key, secret=secret,proxies={'https':'http://127.0.0.1:7890'})
def open():
    try:
        # response = um_futures_client.change_leverage(
        #     symbol="LTCUSDT", leverage=75, recvWindow=6000
        # )
        # logging.info(response)
        response = um_futures_client.new_order(
            symbol="BTCUSDT",
            side="BUY",
            type="MARKET",
            quantity=0.03,
            positionSide="LONG",
        )
        logging.info(response)

    except ClientError as error:
        logging.error(
            "Found error. status: {}, error code: {}, error message: {}".format(
                error.status_code, error.error_code, error.error_message
            )
        )

def open_limit(symbol,price):
    try:
        # response = um_futures_client.change_leverage(
        #     symbol="LTCUSDT", leverage=75, recvWindow=6000
        # )
        # logging.info(response)
        response = um_futures_client.new_order(
            symbol=symbol,
            side="BUY",
            type="LIMIT",
            quantity=0.002,
            positionSide="LONG",
            price = price,
            timeInForce="GTC"
        )
        logging.info(response)

    except ClientError as error:
        logging.error(
            "Found error. status: {}, error code: {}, error message: {}".format(
                error.status_code, error.error_code, error.error_message
            )
        )

def stop(symbol,price):
    try:
        # response = um_futures_client.change_leverage(
        #     symbol="LTCUSDT", leverage=75, recvWindow=6000
        # )
        # logging.info(response)
        response = um_futures_client.new_order(
            symbol=symbol,
            side="SELL",
            type="STOP",
            quantity=0.002,
            positionSide="LONG",
            price = price,
            stopPrice = price - 500
        )
        logging.info(response)

    except ClientError as error:
        logging.error(
            "Found error. status: {}, error code: {}, error message: {}".format(
                error.status_code, error.error_code, error.error_message
            )
        )

def close():
    try:
        # response = um_futures_client.change_leverage(
        #     symbol="LTCUSDT", leverage=75, recvWindow=6000
        # )
        # logging.info(response)
        response = um_futures_client.new_order(
            symbol="BTCUSDT",
            side="SELL",
            type="MARKET",
            quantity=0.03,
            positionSide="LONG",
        )
        logging.info(response)

    except ClientError as error:
        logging.error(
            "Found error. status: {}, error code: {}, error message: {}".format(
                error.status_code, error.error_code, error.error_message
            )
        )

def close_limit():
    try:
        # response = um_futures_client.change_leverage(
        #     symbol="LTCUSDT", leverage=75, recvWindow=6000
        # )
        # logging.info(response)
        response = um_futures_client.new_order(
            symbol="BTCUSDT",
            side="SELL",
            type="LIMIT",
            quantity=0.03,
            positionSide="LONG",
            price=83660,
            timeInForce="GTC"
        )
        logging.warning(response)

    except ClientError as error:
        logging.error(
            "Found error. status: {}, error code: {}, error message: {}".format(
                error.status_code, error.error_code, error.error_message
            )
        )

def close_stop():
    try:
        # response = um_futures_client.change_leverage(
        #     symbol="LTCUSDT", leverage=75, recvWindow=6000
        # )
        # logging.info(response)
        response = um_futures_client.new_order(
            symbol="BTCUSDT",
            side="SELL",
            type="MARKET",
            quantity=0.01,
            positionSide="SHORT",
        )
        logging.warning(response)

    except ClientError as error:
        logging.error(
            "Found error. status: {}, error code: {}, error message: {}".format(
                error.status_code, error.error_code, error.error_message
            )
        )

def read_price():
    klines = um_futures_client.mark_price_klines("BTCUSDT", "1m", **{"limit": 10})
    # logging.info(klines)
    price = round(float(klines[-1][4]),1)
    return price
# open()
# close()
price = read_price()
# open_limit("BTCUSDT",price)
# close_stop()
stop("BTCUSDT",price)

